import Dialog from '@vant/weapp/dialog/dialog';
const { $Toast } = require('../../dist/base/index');
const $api = require('../../utils/api.js').API;
Page({
    data: {
        orderDetail: {},
        publisher: {},
        detail_pic: [],
        state: ['待接单', '已接单', '已完成', '已取消'],
        status: [],
        msg_board: [],
        isCollected: false,
        openid: '',
        type: -1,
        show: false,
        value: '',
        toAccount: ''
    },

    onLoad(e) {
        console.log('orderDetail onload')
        this.setData({
            type: e.type
        })
        wx.getStorage({
            key: 'orderDetail',
            success: (result) => {
                console.log(result.data)
                this.getPublisher(result.data.item)
                this.loadPic(result.data.item)
                this.loadMsg(result.data.item)
                this.setStatus(result.data.item.state)
                this.setData({
                    orderDetail: result.data.item
                })
            },
        });
        wx.getStorage({
            key: 'openid',
            success: (res) => {
                this.setData({
                    openid: res.data
                })
                this.isCollect()
            }
        })
    },

    // 获取订单发布人的信息
    getPublisher(params) {
        $api.getUserinfo(params)
            .then(res => {
                this.setData({
                    publisher: res.data.data[0]
                })
            })
            .catch(err => { console.log(err) })
    },

    // 加载详情图片
    loadPic(params) {
        console.log(params)
        $api.loadPic({
            _id: params._id,
            type: params.type
        })
            .then(res => {
                console.log(res.data.data)
                this.setData({
                    detail_pic: res.data.data
                })
            })
            .catch(err => console.log(err))
    },

    // 加载留言区
    loadMsg(params) {
        console.log(params)
        $api.loadMsg({
            order_id: params._id
        })
            .then(res => {
                // 最新的留言在最上面
                if (res.data.data) {
                    let msg_board = res.data.data[0].message.reverse()
                    this.setData({
                        msg_board
                    })
                }
            })
            .catch(err => console.log(err))
    },

    // 设置状态样式
    setStatus(params) {
        let status = []
        for (let i = 0; i < 4; i++) {
            if (i === params) { status.push('process'); break }
            status.push('finish')
        }
        this.setData({
            status
        })
    },

    // 预览图片
    toBigPic(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.picUrl, // 当前显示图片的http链接
            urls: this.data.detail_pic //  需要预览的图片http链接列表
        })
    },

    // 是否收藏
    isCollect() {
        $api.isCollect({
            openid: this.data.openid,
            order_id: this.data.orderDetail._id
        })
            .then(res => {
                let isCollected = res.data.data.isCollected
                this.setData({
                    isCollected
                })
            })
            .catch(err => console.log(err))
    },

    // 添加收藏
    addCollect() {
        $api.addCollect({
            openid: this.data.openid,
            order_id: this.data.orderDetail._id
        })
            .then(res => {
                this.setData({
                    isCollected: true
                })
                $Toast({
                    content: '收藏成功',
                    type: 'success'
                });
            })
            .catch(err => console.log(err))
    },

    // 取消收藏
    delCollect() {
        $api.delCollect({
            openid: this.data.openid,
            order_id: this.data.orderDetail._id
        })
            .then(res => {
                this.setData({
                    isCollected: false
                })
                $Toast({
                    content: '取消收藏',
                    type: 'success'
                });
            })
            .catch(err => console.log(err))
    },

    // 接单
    takeOrder() {
        $api.takeOrder({
            openid: this.data.openid,
            order_id: this.data.orderDetail._id
        })
            .then(() => {
                Dialog.confirm({
                    title: '接单成功！',
                    message: '你要去往哪里？',
                    confirmButtonText: '查看我的接单',
                    cancelButtonText: '回到订单列表'
                })
                    // 查看我的接单
                    .then(() => {
                        $api.getTaking({ openid: this.data.openid })
                            .then(res => {
                                let takingOrder = JSON.stringify(res.data.data)
                                wx.navigateTo({
                                    url: `/pages/PCT/PCT?order=${takingOrder}&type=1`,
                                });
                            })
                            .catch(err => console.log(err))

                    })
                    // 回到订单列表
                    .catch(() => {
                        wx.switchTab({
                            url: '/pages/home/home',
                        });
                    });
            })
    },

    // 查看交易流程
    toProcess() {
        let orderDetail = JSON.stringify(this.data.orderDetail)
        let publisher = JSON.stringify(this.data.publisher)
        wx.navigateTo({
            url: `/pages/process/process?orderDetail=${orderDetail}&publisher=${publisher}`,
        });
    },

    // 根据不同状况更新订单状态
    updateState(e) {
        let _id = this.data.orderDetail._id
        let cases = parseInt(e.currentTarget.dataset.case)
        $api.updateState({ _id, cases })
            .then(res => {
                $Toast({
                    content: '订单状态已更新',
                    type: 'success'
                });
                console.log(res.data)
            })
            .catch(err => { console.log(err) })
    },

    // 写留言
    writeMsg(e) {
        this.setData({
            value: '',
            show: true,
            toAccount: e.currentTarget.dataset.toaccount
        })
    },

    // 弹窗点击取消
    onClose() { this.setData({ show: false }); },

    // 输入框改动内容
    onChange(e) {
        this.setData({ value: e.detail })
    },

    // 弹窗点击确定
    onConfirm() {
        // 封装参数
        let params = {
            order_id: this.data.orderDetail._id,
            account: this.data.openid,
            isPublisher: this.data.publisher.openid === this.data.openid,
            msg: this.data.value,
            toAccount: this.data.toAccount
        }
        $api.addMsg(params)
            .then(() => {
                $Toast({
                    content: '留言成功',
                    type: 'success'
                });
                this.loadMsg(this.data.orderDetail)
            })
            .catch(err => {
                console.log(err)
                $Toast({
                    content: '留言失败',
                    type: 'error'
                })
            })
    },

    // 去往投诉页
    toComplain() {
        wx.navigateTo({
            url: `/pages/complain/complain?order_id=${this.data.orderDetail._id}`
        })
    }
})